<template>
  <div class="main">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <div class="search">
        <el-form-item label="文件名称" prop="name">
          <el-input v-model.trim="queryParams.name" placeholder="请输入文件名称搜索" maxlength="50" clearable/>
        </el-form-item>
        <el-form-item label="文件描述" prop="description">
          <el-input v-model.trim="queryParams.description" placeholder="请输入文件描述搜索" maxlength="200" clearable/>
        </el-form-item>
        <el-form-item label="创建人" prop="createUser">
          <el-input v-model.trim="queryParams.createUser" placeholder="请输入创建人名称搜索" clearable/>
        </el-form-item>
      </div>
      <div class="search">
        <el-button type="primary" @click="restList(query.keywords)" icon="el-icon-search">查询</el-button>
        <el-button @click="refresh('queryForm')" icon="el-icon-refresh">重置</el-button>
      </div>
    </el-form>
    <div class="create-btn">
      <el-button type="primary" icon="el-icon-plus" @click="openDialog" v-permission="'fileManage:add'">上传文件</el-button>
    </div>
    <div class="create-btn">
      <el-table :data="tableData" element-loading-spinner="el-icon-loading" element-loading-text="加载中。。。"
                element-loading-custom-class="equipment-loading" element-loading-background="rgba(55,55,55,0.2)"
                height="550">
        <el-table-column prop="name" label="文件名称" min-width="200px"/>
        <el-table-column prop="filePath" label="文件地址" min-width="300px" show-overflow-tooltip/>
        <el-table-column prop="description" label="文件描述" show-overflow-tooltip/>
        <el-table-column prop="createUser" label="创建人" width="150px"/>
        <el-table-column prop="createTime" label="上传时间" width="150px"/>
        <el-table-column fixed="right" align="operation" label="操作" width="150px">
          <template slot-scope="scope">
            <el-button type="text" v-if="scope.row.createUser===nickname" @click="deleteFile(scope.row)" v-permission="'fileManage:delete'">
              删除
            </el-button>
            <el-button type="text" id="copy" @click="copy(scope.row)">
              复制链接
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="query.pageNum"
        :limit.sync="query.pageRow"
        layout="total, prev, pager, next"
        :hide-on-single-page="value"
        @pagination="getFileManageList"
      />
    </div>
    <cu-dialog :dialogVisble="visible" @closeDialog="closeDialog" :currentUser="currentUser"
               @refreshList="restList"></cu-dialog>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination/index";
import CuDialog from "./uploadDialog";

export default {
  name: "project",
  components: {Pagination, CuDialog},
  data() {
    return {
      queryParams: {
        name: "",
        description: "",
        createUser: ""
      },
      tableData: [],
      query: {
        pageRow: 10,
        pageNum: 1
      },
      value: false,
      total: 0,
      visible: false,
      currentUser: "",
      nickname: ""
    };
  },
  created() {
    this.currentUser = this.$store.state.user.userId;
    this.nickname = this.$store.state.user.nickname;
  },
  mounted() {
    this.getFileManageList();
  },
  methods: {
    refresh(queryForm) {
      this.$refs[queryForm].resetFields();
      this.getFileManageList();
    },
    restList() {
      this.query.pageNum = 1;
      this.getFileManageList();
      this.closeDialog()
    },
    getFileManageList() {
      this.query = {...this.query, ...this.queryParams}
      this.api({
        url: "/getFileManageList",
        method: "post",
        data: this.query
      }).then(resp => {
        this.tableData = resp.list;
        this.total = resp.totalCount;
      });
    },
    openDialog() {
      this.visible = true;
    },
    closeDialog() {
      this.visible = false;
    },
    deleteFile(data) {
      this.api({url: `/deleteFile/${data.id}`, method: "delete"}).then((resp) => {
        this.getFileManageList()
        this.$message({message: resp, type: 'success'});
      })
    },
    copy(data) {
      this.mes = data.filePath
      //创建input标签
      let input = document.createElement('input')
      //将input的值设置为需要复制的内容
      input.value = this.mes;
      //添加input标签
      document.body.appendChild(input)
      //选中input标签
      input.select()
      //执行复制
      document.execCommand('copy')
      //成功提示信息
      this.$message.success('复制成功')
      //移除input标签
      document.body.removeChild(input)
    }
  }
};
</script>

<style scoped lang="scss">
///deep/ .el-table__empty-block {
//  max-width: 100%;
//padding-right: 100%;
//}

.main {
  height: calc(100vh - 110px);
  padding: 20px;
  background-color: white;
  margin: 20px;
}

.search {
  margin: auto;
  text-align: center
}

.create-btn {
  margin-top: 10px;
}
</style>
